/**
 * author      : dormi330
 * date        : 2018/6/30
 * project     : mybatis
 * description : 应用运行上下文
 * 遇到过很多这样的场景, 与领域无关的东西, 渗透到领域模型里面去
 * 想通过某种方式解决这个问题,
 */

package com.deep.trade.application;

import com.deep.trade.common.TradeException;
import org.springframework.stereotype.Service;

@Service
public class AppRuntimeEvn {

    // 分页信息
    private ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 1;
        }
    };
    private ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 10;
        }
    };

    // 租户编码
    private ThreadLocal<String> tenantCode = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {
            return null;
        }
    };

    // token信息
    private ThreadLocal<String> token = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {
            return null;
        }
    };

    public AppRuntimeEvn ensureToken(String token) throws Exception {
        if (null == token) throw new TradeException("token must not be null");
        this.token.set(token);
        return this;
    }

    public AppRuntimeEvn ensureTenantCode(String tenantCode) throws Exception {
        if (null == tenantCode) throw new TradeException("tenantCode must not be null");
        this.tenantCode.set(tenantCode);
        return this;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum.set(pageNum);
    }


    public void setPageSize(Integer pageSize) {
        this.pageSize.set(pageSize);
    }

    public String getTenantCode() {
        return tenantCode.get();
    }

    public String getToken() {
        return token.get();
    }

//    public void setTenantCode(String tenantCode) {
//        this.tenantCode.set(tenantCode);
//    }


//    public void setToken(String token) {
//        this.token.set(token);
//    }

    public Integer getPageNum() {
        return pageNum.get();
    }

    public Integer getPageSize() {
        return pageSize.get();
    }
}
